<script>
export default {
  created() {
    const { params, query } = this.$route
    const { path } = params

    if (path === 'first-menu') {
      // 查找第一个可用菜单
      const findFirstMenu = (menus) => {
        for (const menu of menus) {
          if (!menu.hidden && menu.path && menu.path !== '/') {
            if (menu.children && menu.children.length > 0) {
              const child = findFirstMenu(menu.children)
              if (child) {
                return {
                  ...child,
                  path: menu.path + '/' + child.path
                }
              }
            }
            return menu
          }
        }
        return null
      }

      const accessRoutes = this.$store.getters.permission_routes || []
      console.log('可用路由:', accessRoutes)
      
      const firstMenu = findFirstMenu(accessRoutes)
      if (firstMenu && firstMenu.path) {
        const path = firstMenu.path.startsWith('/') ? firstMenu.path : '/' + firstMenu.path
        console.log('跳转到第一个菜单:', path)
        this.$router.replace({ path })
      } else {
        this.$message.error('未找到可用菜单')
        this.$router.replace({ path: '/404' })
      }
    } else {
      // 原有的重定向逻辑
      this.$router.replace({ path: '/' + path, query })
    }
  }
}
</script>